﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class KhachHangDAO
    {
        public List<KhachHangDTO> LayDanhSachKhachHang(string connect)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachKhachHang", null);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<KhachHangDTO> list = new List<KhachHangDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    KhachHangDTO khachHang = new KhachHangDTO();
                    khachHang.MaKH = dt.Rows[i]["MTK"].ToString();
                    khachHang.HoTen = dt.Rows[i]["HoTen"].ToString();
                    khachHang.DiaChi = dt.Rows[i]["DiaChi"].ToString();
                    khachHang.DienThoai = dt.Rows[i]["DienThoai"].ToString();
                    khachHang.TienNo = (float)dt.Rows[i]["TienNo"];
                    khachHang.DaXoa = (bool)dt.Rows[i]["DaXoa"];
                    list.Add(khachHang);
                }
            }
            return list;
        }
        public string ThemKhachHang(KhachHangDTO khachHang, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemKhachHang",   new SqlParameter("@hoTenKH", khachHang.HoTen),
                                                             new SqlParameter("@diaChi", khachHang.DiaChi),
                                                             new SqlParameter("@dienThoai", khachHang.DienThoai),
                                                             new SqlParameter("@tienNo", khachHang.TienNo), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }

        public string SuaKhachHang(KhachHangDTO khachHang, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("SuaKhachHang", new SqlParameter("@maKH", khachHang.MaKH),
                                                             new SqlParameter("@hoTenKH", khachHang.HoTen),
                                                             new SqlParameter("@diaChi", khachHang.DiaChi),
                                                             new SqlParameter("@dienThoai", khachHang.DienThoai),
                                                             new SqlParameter("@tienNo", khachHang.TienNo),
                                                              new SqlParameter("@daXoa", khachHang.DaXoa), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
        public string XoaKhachHang(KhachHangDTO khachHang, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaKhachHang", new SqlParameter("@maKH", khachHang.MaKH), par);


            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}